//
//  GlobalSettingUtils.m
//  PhotoFrame
//
//  Created by William on 11-5-8.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "GlobalSettingUtils.h"


@implementation GlobalSettingUtils

+ (NSMutableDictionary *) getGlobalConf {
	// load properties from user document folder
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* sandBoxpath = [documentsDirectory stringByAppendingPathComponent:@"globalconf.plist"];
	NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:sandBoxpath];
	
	// load from app default resources folder
	if (dict == nil) {
		NSString *path = [[NSBundle mainBundle] pathForResource:@"globalconf" ofType:@"plist"];
		dict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
	}
	
	return [dict autorelease];
}

+ (void) setGlobalConfForValue:(id)value forKey:(id) key {
	NSMutableDictionary *dict = [GlobalSettingUtils getGlobalConf];
	
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *sandBoxpath = [documentsDirectory stringByAppendingPathComponent:@"globalconf.plist"];
	
	[dict setObject:value forKey:key];
	[dict writeToFile:sandBoxpath atomically:YES];
}

+ (NSMutableDictionary *) getPlayList {
	// load properties from user document folder
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *sandBoxpath = [documentsDirectory stringByAppendingPathComponent:@"playlist.plist"];
    NSLog(@"for read =====> %@", sandBoxpath);
	NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:sandBoxpath];
    
    if (dict == nil) {
		NSString *path = [[NSBundle mainBundle] pathForResource:@"playlist" ofType:@"plist"];
		dict = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
	}
	return [dict autorelease];
}

+ (void) setPlayListForValue:(id)value forKey:(id) key {    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *sandBoxpath = [documentsDirectory stringByAppendingPathComponent:@"playlist.plist"];
	
    NSLog(@"for write =====> %@", sandBoxpath);
    
	NSMutableDictionary *dict = [GlobalSettingUtils getPlayList];
	[dict setObject:value forKey:key];
    [dict writeToFile:sandBoxpath atomically:YES];
}

+ (void) removePlayListForKey:(id)key {
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *sandBoxpath = [documentsDirectory stringByAppendingPathComponent:@"playlist.plist"];
	
	NSMutableDictionary *dict = [GlobalSettingUtils getPlayList];
	[dict removeObjectForKey:key];
	[dict writeToFile:sandBoxpath atomically:YES];
}
	

- (void) dealloc {
	[super dealloc];
}

@end
